package com.word.chilla.sdk;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.applovin.sdk.AppLovinMediationProvider;
import com.applovin.sdk.AppLovinPrivacySettings;
import com.applovin.sdk.AppLovinSdk;
import com.applovin.sdk.AppLovinSdkConfiguration;
import com.applovin.sdk.AppLovinSdkInitializationConfiguration;
import com.applovin.sdk.AppLovinSdkSettings;
import com.word.chilla.sdk.ad.WChillMaxReward;
import com.word.chilla.sdk.utils.WChillUtils;
import com.word.chilla.sdk.utils.WChillToastUtil;

public class WChillSDKManager implements Application.ActivityLifecycleCallbacks {

    public static WChillSDKManager getInstance() {
        return InstanceHolder.GAME_APPLICATION;
    }

    private static class InstanceHolder {
        private static final WChillSDKManager GAME_APPLICATION = new WChillSDKManager();
    }

    public void init(Activity activity) {
        Log.e("WChill", "onCreateonCreate");
        Context mApplication = activity.getApplicationContext();
        registerActivityLifecycleCallbacks(mApplication);
        WChillBusiness.init(mApplication);

        initAd(mApplication);

        WChillToastUtil.init(activity);
    }

    private void initAd(Context mApplication) {
        setAppLovinPrivacySettings(mApplication);
        setAppLovinSdkSettings(mApplication);
        initMax(mApplication);
    }

    private void initMax(Context mApplication) {
        AppLovinSdkInitializationConfiguration initConfig = AppLovinSdkInitializationConfiguration.builder(
                        WChillUtils.decrypt(WChillConstant.MAX_KEY), mApplication)
                .setMediationProvider(AppLovinMediationProvider.MAX)
                .build();
        AppLovinSdk.getInstance(mApplication).initialize(initConfig, new AppLovinSdk.SdkInitializationListener() {
            @Override
            public void onSdkInitialized(final AppLovinSdkConfiguration config) {
//                AppLovinSdk.getInstance(context).showMediationDebugger();
                Log.e("WChill", "onSdkInitialized");
                WChillMaxReward.getInstance().loadRewardAd(mApplication);
            }
        });
    }

    private void setAppLovinSdkSettings(Context mApplication) {
        AppLovinSdkSettings settings = AppLovinSdk.getInstance(mApplication).getSettings();
        settings.setExtraParameter("enable_black_screen_fixes", "true");
    }

    private void setAppLovinPrivacySettings(Context mApplication) {
        AppLovinPrivacySettings.setHasUserConsent(true, mApplication);
        AppLovinPrivacySettings.setDoNotSell(false, mApplication);
    }

    private void registerActivityLifecycleCallbacks(Context mApplication) {
        ((Application) mApplication).registerActivityLifecycleCallbacks(this);
    }

    @Override
    public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
        WChillBusiness.onActivityCreate(activity);
    }

    @Override
    public void onActivityStarted(@NonNull Activity activity) {

    }

    @Override
    public void onActivityResumed(@NonNull Activity activity) {

    }

    @Override
    public void onActivityPaused(@NonNull Activity activity) {

    }

    @Override
    public void onActivityStopped(@NonNull Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(@NonNull Activity activity) {

    }





}
